package com.example.trueordare.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.trueordare.R;
import com.example.trueordare.tools.QuestionTools;

public class QuestionActivity extends AppCompatActivity implements View.OnClickListener,TextWatcher {
    private Button mNext;    //下一步按钮
    private Button mFinish;  //完成按钮
    private TextView mText;  //问题显示的文本 TextView
    private final QuestionTools tools = QuestionTools.getInstance();//创建问题工具类
    private int index;       //记录当前问题的索引
    private EditText mEditAnswer;  //回答问题的输入框
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        index = 0;
        setContentView(R.layout.activity_question);       //加载布局
        mText = findViewById(R.id.question_text);         //获取 问题显示的文本 TextView
        mNext = findViewById(R.id.button_next);           //获取"下一步"按钮对象
        mFinish = findViewById(R.id.button_finish);       //获取"完成 "按钮对象
        mNext.setOnClickListener(this);                   //设置点击监听器
        mEditAnswer = findViewById(R.id.edit_answer);     //获取编辑框对象
        mEditAnswer.addTextChangedListener(this);      //添加文本改变监听器
        mFinish.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_next:
                //点击下一步按钮 显示下一个问题 直到最后一个问题
                mText.setText(tools.getQuestions()[++index].getQuestionId());
                //清空编辑框
                mEditAnswer.setText("");

                //最后一个问题后隐藏改按钮显示 "完成"按钮
                if (index == tools.getQuestions().length - 1){
                    mNext.setVisibility(View.GONE);
                    mFinish.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.button_finish:
                Intent intent = AnswerListActivity.newIntent(QuestionActivity.this);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //输入完成后 将答案保存
        tools.getQuestions()[index].setAnswer(s.toString());
    }

    @Override
    public void afterTextChanged(Editable s) {



    }
}